const powerDao = require('../dao/powerDao.js');

const powerService = {
    // 鉴权接口 用于判断某个用户类型是否有某个权限
    async checkPower(userTypeId, power) {
        let result;
        try {
            result = await powerDao.checkPower(userTypeId, power);
            if (result.length) {
                return { code: 200, msg: "成功", data: result[0] };
            } else {
                return { code: 403, msg: "失败", data: null };
            }
        } catch (err) {
            return { code: 500, msg: "服务器错误", data: null };
        }
    },
    // 获取用户类型
    async getUserType(userTypeId) {
        let result;
        try {
            result = await powerDao.getUserType(userTypeId);
            if (result.length) {
                return { code: 200, msg: "获取成功", data: result[0] };
            } else {
                return { code: 500, msg: "获取失败", data: null };
            }
        } catch (err) {
            return { code: 500, msg: "服务器错误", data: null };
        }
    },
    // 查看当前的token令牌对应的用户是否存在
    async getUser(id) {
        let result;
        try {
            result = await powerDao.getUser(id);
            if (result.length) {
                return { code: 200, msg: "获取成功", data: result[0] };
            } else {
                return { code: 500, msg: "获取失败", data: null };
            }
        } catch (err) {
            return { code: 500, msg: "服务器错误", data: null };
        }
    }
};

module.exports = powerService;
